package com.sofwin.controller;

import com.sofwin.entity.AjaxResult;
import com.sofwin.pojo.SysDept;
import com.sofwin.service.DeptService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import java.util.List;

/**
 * ClassName: DeptController
 * Description: 部门的请求
 * date: 2022/4/27 9:53 上午
 *
 * @author andyliu
 * @qq 115176513
 * @since JDK 1.8
 */
@Controller
@RequestMapping("/dept")
@Api(tags = "部门管理",produces = "application/json")
public class DeptController {
    @Autowired
    private DeptService deptService;
    @GetMapping()
    // 这个请求不会出现在文档中
    @ApiIgnore
    public String toData(){
        return "dept/data";
    }
    @GetMapping("/toEdit")
    @ApiIgnore
    public String toEidt(Integer id, ModelMap map,Integer parentId){
        List<SysDept> depts = deptService.queryDepts(null);
        map.put("depts",depts);
        if(parentId!=null){
            SysDept dept = new SysDept();
            dept.setParentId(parentId);
            map.put("dept",dept);
        }
        if(id!=null){
            SysDept dept = deptService.queryById(id);
            if(dept.getParentId()==0){
                map.put("parentName","无上级部门");
            }else{
                SysDept parentDept = deptService.queryById(dept.getParentId());
                map.put("parentName",parentDept.getDeptName());

            }
            map.put("dept",dept);
        }
        return "dept/edit";
    }
    @GetMapping("/tree")
    @ApiIgnore
    public String tree(){
        return "dept/tree";
    }
    @PostMapping("/data")
    @ResponseBody
    @ApiOperation(value = "根据条件查询部门列表",httpMethod = "POST",produces = "application/json")
    public AjaxResult data(SysDept dept){
        List<SysDept> depts = deptService.queryDepts(dept);
        return AjaxResult.success(depts);
    }
    @GetMapping("/info/{id}")
    @ResponseBody
    @ApiOperation(value = "根据id查询部门信息",httpMethod = "GET",produces = "application/json")
    public AjaxResult info(@PathVariable Integer id){
        SysDept sysDept = deptService.queryById(id);
        return AjaxResult.success(sysDept);
    }
    @PostMapping("save")
    @ResponseBody
    @ApiOperation(value = "保存部门信息",httpMethod = "POST",produces = "application/json")
    public AjaxResult save(SysDept dept){
        boolean status = deptService.saveOrUpdate(dept);
        if(status){
            return AjaxResult.success(status);
        }
       return AjaxResult.error(status);
    }
    @DeleteMapping("/delete/{id}")
    @ResponseBody
    @ApiOperation(value = "根据id删除部门",httpMethod = "DELETE",produces = "application/json")
    public AjaxResult delete(@PathVariable Integer id){
        boolean status = deptService.removeById(id);
        if(status){
            return AjaxResult.success(status);
        }
        return AjaxResult.error(status);
    }

    @DeleteMapping("/deletes")
    @ResponseBody
    @ApiOperation(value = "批量删除部门",httpMethod = "DELETE",produces = "application/json")
    public AjaxResult delete(Integer[] ids){
        boolean status = deptService.removeByIds(ids);
        if(status){
            return AjaxResult.success(status);
        }
        return AjaxResult.error(status);
    }
}
